package org.genntii.warehousesystem.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.genntii.warehousesystem.common.Result;
import org.genntii.warehousesystem.domain.dto.OrderDTO;
import org.genntii.warehousesystem.domain.vo.OrderVO;
import org.genntii.warehousesystem.service.GoodsOrderService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "订单接口")
@RestController
@RequestMapping("/goodsOrder")
public class GoodsOrderController {

    @Resource
    private GoodsOrderService goodsOrderService;

    @Operation(summary = "创建订单")
    @PostMapping("/create")
    public Result create(HttpServletRequest request, @RequestBody OrderDTO dto){
        int userId = (int) request.getAttribute("id");
        goodsOrderService.createOrder(dto.getGoodsId(),dto.getNum(),dto.getWarehouseId(),userId,dto.getPricePer(),dto.getAddressId());
        return Result.success();
    }

    @Operation(summary = "更新订单状态(管理端接口)",description = "提供给管理端，包括已发货等")
    @Parameters({
            @Parameter(name = "id",description = "订单id",required = true,in = ParameterIn.QUERY),
            @Parameter(name = "status",description = "状态编号",required = true,in = ParameterIn.QUERY)
    })
    @PostMapping("/v1/statusUpdate")
    public Result statusUpdate(@RequestParam("id") int id,@RequestParam("status") int status){
        goodsOrderService.statusUpdate(id,status);
        return Result.success();
    }
    @Operation(summary = "更新订单状态(用户接口)")
    @Parameters({
            @Parameter(name = "id",description = "订单id",required = true,in = ParameterIn.QUERY),
            @Parameter(name = "status",description = "状态编号",required = true,in = ParameterIn.QUERY)
    })
    @PostMapping("/statusUpdate")
    public Result userStatusUpdate(@RequestParam("id") int id,@RequestParam("status") int status){
        goodsOrderService.statusUpdate(id, status);
        return Result.success();
    }

    @Operation(summary = "分页展示订单",description = "管理端")
    @Parameter(name = "page",description = "页码",required = true,in = ParameterIn.QUERY)
    @GetMapping("/list")
    public Result<List<OrderVO>> list(@RequestParam("page")int page, HttpServletRequest request){
        int userId = (int) request.getAttribute("id");
        return Result.success(goodsOrderService.selectOrderByPage(userId,10*(page-1)));

    }

    @Operation(summary = "分页展示订单",description = "用户端")
    @Parameters({
            @Parameter(name = "page",description = "页码",required = true,in = ParameterIn.QUERY),
            @Parameter(name = "warehouseId",description = "仓库id",required = true,in = ParameterIn.QUERY)
    })
    @GetMapping("/v1/listByWarehouse")
    public Result<List<OrderVO>> listByWarehouse(@RequestParam("page")int page, @RequestParam("warehouseId") int warehouseId){
        return Result.success(goodsOrderService.selectOrderByPageForWarehouse(warehouseId,page));
    }

}
